package com.huiyujiaoyu.powertime.ui.login;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.huiyujiaoyu.base.BaseActivity;
import com.huiyujiaoyu.powertime.MainActivity;
import com.huiyujiaoyu.powertime.R;
import com.huiyujiaoyu.powertime.model.ConfigBean;
import com.huiyujiaoyu.powertime.model.UserInfoBean;
import com.huiyujiaoyu.util.ActivityUtil;
import com.huiyujiaoyu.util.GlideUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author: dong.yj
 * created on: 2016/11/29 11:45
 * description:
 */
public class GuidePageActivity extends BaseActivity {

    @BindView(R.id.vp_guide)
    ViewPager mGuidePages;
    @BindView(R.id.btn_pass)
    Button mBtnPass;
    private int currentItem = 0; // 记录当前显示页面的位置
    private int[] picss = {R.drawable.yindaoye_1, R.drawable.yindaoye_2, R.drawable.yindaoye_3, R.drawable.yindaoye_4};// 图片资源ID
    private ArrayList<View> viewList = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_guidepage;
    }

    @Override
    protected void initView() {
        fillGuanggao(null,picss);
    }
    /**
     * 添加自动滑动图片
     *
     * @param arrayList 图片集合
     */
    public void fillGuanggao(List<ConfigBean> cb, int[] arrayList) {
        viewList.clear();
        int size = cb==null?arrayList.length:cb.size();
        for (int i = 0; i < size; i++) {
            ImageView iv = new ImageView(this);
            iv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
            GlideUtil.loadImg(iv,arrayList[i%4]);
            if(cb!=null){
                GlideUtil.loadImg2(iv,cb.get(i).image);
            }
            if(i==size-1){
                iv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        UserInfoBean.getInstance().setFirstOpen(false);
                        ActivityUtil.startActivity(mContext, MainActivity.class);
                        finish();
                    }
                });
            }
            iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
            viewList.add(iv);
        }
        mGuidePages.setAdapter(new MyViewPagerAdapter(viewList));
    }


    @Override
    protected void initListener() {
        mGuidePages.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                currentItem = position;
                if (currentItem == picss.length - 1) {
                    mBtnPass.setVisibility(View.GONE);
                } else {
                    mBtnPass.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    @Override
    protected void initData() {

    }
    @Override
    public void onNext(Object o, int httpcode) {

    }

    public class MyViewPagerAdapter extends PagerAdapter {
        private List<View> mListViews;

        public MyViewPagerAdapter(List<View> mListViews) {
            this.mListViews = mListViews;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mListViews.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mListViews.get(position), 0);
            return mListViews.get(position);
        }

        @Override
        public int getCount() {
            return mListViews.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }
    }

    @OnClick({R.id.btn_pass})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_pass:
                UserInfoBean.getInstance().setFirstOpen(false);
                ActivityUtil.startActivity(mContext,LoginActivity.class);
                finish();
                break;
        }
    }
}
